package jmine.tec.batch;

import java.util.concurrent.Callable;

import jmine.tec.batch.context.BatchExecutionContext;

/**
 * Implementaçao do {@link Callable} onde os valores do {@link BatchExecutionContext} do {@link BatchController} é atualizado antes do call
 * ser chamado.
 * 
 * @author Willian Gigliotti
 * @param <V>o resultado do método call e doCall
 */
public abstract class BatchExecutionContextAwareCallable<V> implements Callable<V> {

    private final BatchController controller;

    private final BatchExecutionContext context;

    /**
     * Construtor.
     * 
     * @param controller {@link BatchController}, para pegar o {@link BatchExecutionContext}
     */
    public BatchExecutionContextAwareCallable(BatchController controller) {
        this.controller = controller;
        this.context = controller.getBatchExecutionContext();
    }

    /**
     * {@inheritDoc}
     */
    public final V call() throws Exception {
        this.controller.getBatchExecutionContext().addAll(this.context);
        return this.doCall();
    }

    /**
     * Analogo ao {@link Callable#call()}
     * 
     * @return resultado computado
     * @throws Exception se algo der errado
     */
    protected abstract V doCall() throws Exception;

}
